<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: FileController.php 4761 2010-08-21 18:52:33Z huuphuoc $
 * @since		2.0.0
 */

class Core_MediaController extends Zend_Controller_Action 
{

	public function uploadAction()
	{
		$allowedExtensions = array("jpg","jpeg","png","gif");
		$sizeLimit = Tomato_Config::getConfig()->get("manager")->maxFileSize * 1024 * 1024;
		
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		try{
			$request = $this->getRequest();
			if (!$request->isPost()) {
				throw new Exception("Lỗi phương thức");
			}else{
				$shop_id		  = $request->getParam("shop_id");	
				$resource_type    = $request->getParam("resource_type");
				$resource_id 	  = $request->getParam("resource_id");
				
				if($shop_id!=Zend_Auth::getInstance()->getIdentity()->shop->id){
					throw new Exception("Bạn không có quyền upload vào thư mục tài nguyên của người khác");	
				}	
				
				$dir  			= TOMATO_ROOT_DIR;
				$path 			= 'resource' . DS . substr(Tomato_Config::getConfig()->get('db')->prefix,0,-1) . DS . $shop_id. DS.'images'.DS . $resource_type . DS . $resource_id;
				$path_relative  = 'resource' . '/' . substr(Tomato_Config::getConfig()->get('db')->prefix,0,-1) . '/' . $shop_id.'/'.'images/'.$resource_type.'/'. $resource_id.'/';
				Tomato_Utility_File::createDirs($dir, $path);
		
				$uploader = new Core_Services_FileUploader($allowedExtensions, $sizeLimit);
				$result = $uploader->handleUpload($path_relative);
				if($result && $result["success"]){
					$ret   = array(
						'success'	=> true,
						'message'	=> $this->view->translator("message_upload_success"),	
						'url'  		=> $this->view->APP_STATIC_SERVER.'/'.$path_relative . $result["fileName"],
						'fileName'	=> $result["fileName"]
					);
				}else{
					throw new Exception($result["error"]);
				}
			}
		}catch (Exception $ex){
				$ret   = array(
					'error'	=> 'Lỗi xảy ra trong quá trình upload : '.$ex->getMessage()
				);			
		}
		echo Zend_Json::encode($ret);
		die;
	}
	public function deleteAction(){
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		try{
			$request = $this->getRequest();
			if (!$request->isPost()) {
				throw new Exception("Lỗi phương thức");
			}else{
				$shop_id		  = $request->getParam("shop_id");	
				$resource_type    = $request->getParam("resource_type");
				$resource_id 	  = $request->getParam("resource_id");
				$fileName         = $request->getParam("fileName");
				
				if($shop_id!=Zend_Auth::getInstance()->getIdentity()->shop->id){
					throw new Exception("Bạn không có quyền xóa hình ảnh trong tài nguyên của người khác");	
				}	
								
				$dir  			= TOMATO_ROOT_DIR;
				$path 			= 'resource' . DS . substr(Tomato_Config::getConfig()->get('db')->prefix,0,-1) . DS . $shop_id. DS.'images'.DS . $resource_type . DS . $resource_id;
				$path_relative  = 'resource' . '/' . substr(Tomato_Config::getConfig()->get('db')->prefix,0,-1) . '/' . $shop_id.'/'.'images/'.$resource_type.'/'. $resource_id;
				$url_delete     = $path_relative  .'/'. $fileName;
				echo $url_delete; die;
			}
		}catch(Exception $ex){
				$ret   = array(
					'error'	=> 'Lỗi xảy ra trong quá trình upload : '.$ex->getMessage()
				);						
		}
		echo Zend_Json::encode($ret);
		die;		
	}	
}